package com.pipi.mq.rocketmq.base.producer;

import com.pipi.mq.rocketmq.base.config.PropertiesConfig;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.*;

/**
 * @Package com.pipi.mq.rocketmq.base
 * @author: 皮皮（pipi@maihaoche.com）
 * @date: 2020/4/18 3:10 PM
 */
@Component
public class OneWayProducer extends AbstractMQProducer {

    private int n = 1000;

    public OneWayProducer() {
        setTopic(PropertiesConfig.topicOneWay);
        setTag(PropertiesConfig.tagOneWay);
    }

    public void oneWaySendThread(CountDownLatch countDownLatch) {
        ExecutorService executorService = new ThreadPoolExecutor(1,
                1,
                100L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(10));
        executorService.submit(() -> {
            oneWaySend();
            countDownLatch.countDown();
        });
        executorService.shutdown();
    }

    /**
     * 发送单向消息
     */
    public void oneWaySend() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            String s = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + "  oneWaySendMessage 发送单向消息-" + i;
            sendOneWay("", s,s);
        }
        System.out.println(System.currentTimeMillis() - startTime);
    }

}
